package com.work.service.impl;

import com.work.entity.Weather;
import com.work.entity.WeatherDay;
import com.work.entity.WeatherHour;
import com.work.service.WeatherService;
import com.work.utils.DbUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import sun.rmi.runtime.Log;

import java.util.*;

/**
 * Created by baichenguang on 2020/2/18.
 */
public class WeatherServiceImpl implements WeatherService {


    public List<Weather> getWeatherList(int pageSize,int curPage) {

        List<Weather> weathers = new ArrayList<Weather>();
        Session session = DbUtil.getSession();
        try {

            //查询主表信息：
            Criteria criteria = session.createCriteria(Weather.class);
            // 放置分页查询的起始坐标
            criteria.setFirstResult(curPage);
            // 放置每页查询记录记录数
            criteria.setMaxResults(pageSize);
            // 返回查询结果并返回给集合
            weathers = criteria.list();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.close();
        }
        return weathers;
    }

    public List<Weather> getWeatherListByDate(Date startTime, Date endTime) {
        List<Weather> weathers = new ArrayList<Weather>();
        Session session = DbUtil.getSession();
        try {
            //查询主表信息：
            Criteria criteria = session.createCriteria(Weather.class);
            criteria.add(Restrictions.between("date", startTime, endTime));
            weathers = criteria.list();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.close();
        }
        return weathers;
    }

    /**
     * 获取月份的数据（每天的平均值）
     * @param y
     * @param m
     * @return
     */
    public List<WeatherDay> getWeatherListByMonth(int y, int m) {
        List<WeatherDay> weathers = new ArrayList<WeatherDay>();
        Session session = DbUtil.getSession();
        try {
            //查询主表信息：
            Map<String,Integer> parms = new HashMap<String, Integer>();
            parms.put("year",y);
            parms.put("month",m);
            Criteria criteria = session.createCriteria(WeatherDay.class);
            criteria.add(Restrictions.allEq(parms));
            criteria.addOrder(Property.forName("day").asc());
            weathers = criteria.list();
            return weathers;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.close();
        }
    }

    /**
     * 获取指定天的小时数据（每个小时的平均）
     * @param y
     * @param m
     * @param d
     * @return
     */
    public List<WeatherHour> getWeatherListByDay(int y, int m, int d) {
        List<WeatherHour> weathers = new ArrayList<WeatherHour>();
        Session session = DbUtil.getSession();
        try {
            //查询主表信息：
            Map<String,Integer> parms = new HashMap<String, Integer>();
            parms.put("year",y);
            parms.put("month",m);
            parms.put("day",d);
            Criteria criteria = session.createCriteria(WeatherHour.class);
            criteria.add(Restrictions.allEq(parms));
            weathers = criteria.list();
            return weathers;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.close();
        }
    }

}
